package com.app.community.mapper;

import com.app.community.model.entity.Question;
import org.apache.ibatis.annotations.*;


/**
 *  2020/10/20 11:45
 **/
@Mapper
public interface CardMapper {


    /**
     * 与publishMapper中方法相同，但查询的信息更少
     */
    @Results({
            @Result(column = "question_id",property = "questionId"),
            @Result(column = "browse_count",property = "browseCount")
    })
    @Select({"select title,article,creator,browse_count from question where question_id = #{questionId}"})
    Question selectQuestionById(@Param("questionId")Long questionId);

    /**
     * 点赞
     */
    @Insert({"insert into like_list(user_id, date, question_id) values(#{userId},#{date},#{questionId})"})
    void like(@Param("userId")String userId,@Param("date")String date,@Param("questionId")Long questionId);

    /**
     * 删除点赞
     */
    @Delete({"delete from like_list where user_id = #{userId} and question_id = #{questionId}"})
    void dislike(@Param("userId")String userId,@Param("questionId")Long questionId);


    /**
     * 查询是否点赞,点赞则为1，未点赞则为null
     */
    @Select({"select 1 from like_list where user_id = #{userId} and question_id = #{questionId}"})
    Integer ifLike(@Param("userId")String userId,@Param("questionId")Long questionId);

    /**
     * 点赞数量
     */
    @Select({"select count(*) from like_list where question_id = #{questionId}"})
    Long likeCount(@Param("questionId")Long questionId);


    /**
     * 更新文章浏览量
     */
    @Update({"update question set browse_count = browse_count + #{browseCount} where question_id = #{questionId}"})
    void updateQuestionViewCount(@Param("browseCount")Integer browseCount,@Param("questionId")Long questionId);
}
